package liurong.driver.concurrent;

import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class CallableAndFuture {
	public static void main(String[] args) throws Exception {
		Callable<String> callable = new Callable<String>() {
			public String call() throws Exception {
				System.out.println("call");
				TimeUnit.SECONDS.sleep(1000);
				return UUID.randomUUID().toString();
			}
		};

		FutureTask<String> future = new FutureTask<String>(callable);

		Runnable myThread = new MyThread(future);
		new Thread(myThread).start();

		System.out.println("-");
	}

	public static class MyThread implements Runnable {

		private FutureTask<String> futureTask;

		public MyThread(FutureTask<String> future) {
			this.futureTask = future;
		}

		public void run() {
			try {
				new Thread(futureTask).start();
				System.out.println("future:" + futureTask.get(3, TimeUnit.SECONDS));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
